package com.wxzz.elearning.common.exception;

import com.wxzz.elearning.common.vo.ReturnVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalExceptionHandler {

    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 业务异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public ReturnVO serviceExceptionHandler(ServiceException e) {
        ReturnVO returnVO = new ReturnVO();
        returnVO.setCode("500");
        returnVO.setMessage(e.getMessage());
        return returnVO;
    }

    /**
     * @Notnull异常
     * @param methodArgumentNotValidException
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ReturnVO throwCustomException(MethodArgumentNotValidException methodArgumentNotValidException) {
        ReturnVO returnVO = new ReturnVO();
        returnVO.setCode("500");
        returnVO.setMessage(methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
        return returnVO;
    }

//
//
//    @ExceptionHandler
//    public ReturnVO allExceptionHandler(Exception e) {
//        ReturnVO returnVO = new ReturnVO();
//        returnVO.setCode("900");
//        returnVO.setMessage(e.getMessage());
//        logger.error(e.getMessage());
//        System.err.println(e.getMessage());
//        return returnVO;
//    }


}
